//
//  IvanNetStreamTEST.m
//  YuchBox
//
//  Created by Ivan on 11-10-9.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//


#import "IvanNetStreamTEST.h"

#ifdef IVAN_DEBUG
@implementation IvanNetStreamTEST

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

+ (void)TEST_beginEnable:(Boolean)enable;
{
    if (!enable) return;
    //[IvanNetStreamTEST TEST_connect];
//    [IvanNetStreamTEST TEST_sendData];
    [IvanNetStreamTEST TEST_recvEnable:NO];
    [IvanNetStreamTEST TEST_sendEmailEnable:NO];
    [IvanNetStreamTEST TEST_sendEmailAndAttachmentEnable:YES];
}

+ (void)TEST_connect
{
    Boolean ret = [IVAN_NET_STREAM connectWithServer:@"192.168.10.10" port:9716 delegate:nil];
    assert(ret == TRUE);
    NSLog(@"[TEST]connect successful");
   // sleep(3);
}

+ (void)TEST_sendData
{
    //[IVAN_NET_STREAM connectTest];
    //[IVAN_NET_STREAM requestData:@"1111" delegate:nil flag:1];
    // buffer test
  //  NSInteger inValue1 = 0x11223344;
  //  [IVAN_NET_STREAM preWriteInt32:inValue1];
  //  [IVAN_NET_STREAM writeFlush];

//    NSString *stringValue1 = @"abcdefghijkimnopqrstuvwxyz";
//    NSString *stringValue2 = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//    [IVAN_NET_STREAM preWriteString:stringValue1];
//    [IVAN_NET_STREAM preWriteString:stringValue2];
//    [IVAN_NET_STREAM writeFlush];
//    NSString *stringValue3CN = @"啊啊啊哈哈哈\n";
//    [IVAN_NET_STREAM preWriteString:stringValue3CN];
//    //[IVAN_NET_STREAM writeFlush];
//    NSString *stringValue4 = @"44444444\n";
//    [IVAN_NET_STREAM preWriteString:stringValue4];
    
    
    // TEST when buffer full
//    NSString *stringValue5 = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//    [IVAN_NET_STREAM preWriteString:stringValue5];
    
    //[IVAN_NET_STREAM preWriteString:stringValue5];
//    [IVAN_NET_STREAM writeFlush];
    
}

+ (void)TEST_recvEnable:(Boolean)enable
{
    if (!enable) return;
    UInt8 in1[32] = {0, 0, 0x1c, 0, 0x18, 0, 0, 0, 0x2, 0x6, 0, 0, 0 ,0x31 ,0x31 ,0x31, 0x31 ,0x31, 0x31, 0x6, 0, 0, 0, 0, 0x3, 0, 0, 0, 0x31, 0x31, 0x31, 0};
    NSData *nsdata1 = [NSData dataWithBytes:in1 length:32];
    TTDUMP((UInt8 *)[nsdata1 bytes], 32);
    NSInputStream * inStream = [NSInputStream inputStreamWithData:nsdata1];
    [inStream open];
    //NSInteger x;
    
    //[inStream read:(UInt8 *)&x maxLength:4];
    
    //TTDUMP((UInt8 *)&x, 4);
    [DATA_SOURCE readInStream:inStream];
}

+ (void)TEST_sendEmailEnable:(Boolean)enable
{
//    
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
//    
//    NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
//    [dateformat setTimeStyle:NSDateFormatterMediumStyle];
//    [dateformat setDateStyle:NSDateFormatterMediumStyle];
//    
//    
//    NSLog(@"%@", [dateformat stringFromDate:date]);
////    
////    return;
//    
//    NSString *x = @"22222";
//    //TTLOG(@"1111");
//    //TTLOG(@"%d", 1);
//    //TTLOG(@"%@",x);
//    NSInteger x1 = 1;
   // TTLOGF_ERROR(@"%d %@ 2222", x1, x);
   // TTLOGF_ERROR1(@"%d %@", x1, x);
     if (!enable) return;
    
    // init a mail
    Email *emailA = [[Email alloc] init];
    
    [emailA setVersion:2];
    [emailA setIndex:0];
    [emailA.fromArray addObject:@"flarez@163.com"];
    
    [emailA.toArray addObject:@"flarez@163.com"];
    [emailA setSubject:@"TEST from ios"];
    [emailA setSendDate:[NSDate dateWithTimeIntervalSinceNow:0]];
    [emailA setFlag:0];
    [emailA setXmailName:[NSString stringWithFormat:@"yuchbox"]];
    [emailA setContent:[NSString stringWithFormat:@"TEST from ios content"]];
    [emailA setContentHtml:[NSString stringWithFormat:@""]];
    //[emailA setXmailName:[NSString stringWithFormat:@"yuchbox"]];
    [emailA setHasLocationInfo:NO];
    [emailA setContentHtmlType:[NSString stringWithFormat:@""]];
    //[emailA set
    
   [DATA_SOURCE sendMail:emailA];
    [emailA release];
    
//    sleep(2);
//    Email *emailB = [[Email alloc] init];
//    
//    [emailB setVersion:2];
//    [emailB setIndex:0];
//    [emailB.fromArray addObject:@"flarez@163.com"];
//    
//    [emailB.toArray addObject:@"flarez@163.com"];
//    [emailB setSubject:@"来自IOS的中文测试"];
//    [emailB setSendDate:[NSDate dateWithTimeIntervalSinceNow:0]];
//    [emailB setFlag:0];
//    [emailB setXmailName:[NSString stringWithFormat:@"yuchbox"]];
//    [emailB setContent:[NSString stringWithFormat:@"来自IOS的中文测试"]];
//    [emailB setContentHtml:[NSString stringWithFormat:@""]];
//    //[emailA setXmailName:[NSString stringWithFormat:@"yuchbox"]];
//    [emailB setHasLocationInfo:NO];
//    [emailB setContentHtmlType:[NSString stringWithFormat:@""]];
//    //[emailA set
//    
//
//    [DATA_SOURCE sendMail:emailB]; 
    
//    [emailB release];
    
    //UInt8 * outBuffer = (UInt8 *)malloc(10*1024);
    //NSOutputStream *outStream = [NSOutputStream outputStreamToBuffer:outBuffer capacity:10*1024];
}

+ (void)TEST_sendEmailAndAttachmentEnable:(Boolean)enable
{
    if (!enable) return;
    
    Email *emailA = [[Email alloc] init];
    
    [emailA setVersion:2];
    [emailA setIndex:0];
    [emailA.fromArray addObject:@"flarez@163.com"];
    
    [emailA.toArray addObject:@"flarez@163.com"];
    [emailA setSubject:@"TEST from ios with attachment"];
    //[emailA setSendTimestamp:[NSDate dateWithTimeIntervalSinceNow:0]*1000];
    [emailA setSendDate:[NSDate dateWithTimeIntervalSinceNow:0]];
//    [emailA setSendTimestamp:[emailA.sendDate timeIntervalSinceNow] * 1000];
    [emailA setFlag:0];
    [emailA setXmailName:[NSString stringWithFormat:@"yuchbox"]];
    [emailA setContent:[NSString stringWithFormat:@"TEST from ios content"]];
    [emailA setContentHtml:[NSString stringWithFormat:@""]];
    //[emailA setXmailName:[NSString stringWithFormat:@"yuchbox"]];
    [emailA setHasLocationInfo:NO];
    [emailA setContentHtmlType:[NSString stringWithFormat:@""]];
    
    Attachment *attachmentA = [[Attachment alloc] init];
    
    attachmentA.attachmentFileName = [NSString stringWithFormat:@"%@/D066100C",[Attachment getAttachTmpDictPath]];
    attachmentA.realName           = @"TESTLarge.txt";
    attachmentA.type               = @"application/octet-stream; charset=gbk; name=TESTSmall.txt";
    NSError *error;
    NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:attachmentA.attachmentFileName error:&error];
    NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
    attachmentA.attachmentSize = [fileSize intValue];
    
    [emailA.attachmentArray addObject:attachmentA];
    
    [DATA_SOURCE sendMail:emailA];
    [attachmentA release];
    [emailA release];
}
@end
#endif